/**
 *  *2010-4-17 下午05:00:24  add 
 */
package com.base.myproject.client.busi.ccode;

import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.BaseTabPanel;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 会员查询，支持打印
 * 
 * @author Administrator 2010-4-17 javazgw@gmail.com
 */
public class Ccodequery_service extends BaseJDBCPanel implements Printer,Listener<BaseEvent> {
	com.extjs.gxt.ui.client.widget.FramePanel frame = com.extjs.gxt.ui.client.widget.FramePanel
			.pop();

	Button query=new Button("查询");
	Button print=new Button("打印");
	public TextField<String> s_cardno = new TextField<String>();
	TextField<String> cname = new TextField<String>();
	private Ccodequery_service() {
		super();
		init();

	}
	static Ccodequery_service c_s=null;
	public static void getInstance()
	{
		if(c_s==null)
		{
			c_s= new Ccodequery_service();
			c_s.complete();
			 BaseTabPanel.getInstance().addTab("会员查询",c_s);
		}
		else
		{
			TabItem  t = BaseTabPanel.getInstance().findItem(c_s.getElement());
			if(t!=null)
			{
				BaseTabPanel.getInstance().setSelection(t);
				((Ccodequery_service)t.getWidget(0)).s_cardno.focus();
				
			}
			else
			{
				BaseTabPanel.getInstance().addTab("会员查询",c_s);
				c_s.s_cardno.focus();
			}
				
		}
	}

	public void init() {

		// this.setAutoHeight(true);
		this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeigetx(), centerData);

		this.setBusino(busi);
		

	}
	public void complete()
	{
		super.complete();
		
		getToolbarll().clear();
		getToolbarll().add(print);
	}
	private Widget initWeigetx() {
		
		print.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		query.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.search_24()));
		frame.setId("__printingFrameX_Ccodequery_service");
		ContentPanel panel = new ContentPanel();
		
		panel.setLayout(new RowLayout(Orientation.VERTICAL));  
//		d.add(printlist,new RowData(0.2, 1));
//		d.add(frame,new RowData(0.8, 1));
		
		
//		VerticalPanel v = new VerticalPanel();
//
//		v.setHorizontalAlign(HorizontalAlignment.LEFT);

		panel.add(buttongroup(),new RowData(1, 0.1));
		panel.add(frame,new RowData(1, 0.9,new Margins(10) ));
		//panel.setAutoHeight(true);
		panel.setHeaderVisible(false);
		//frame.setHeight("400px");
		panel.setHeight("400px");
		//frame.setBorders(false);
		s_cardno.focus();
		s_cardno.setSelectOnFocus(true);
		return panel;

	}

	Widget buttongroup() {
		HorizontalPanel h = new HorizontalPanel();
		h.add(new Label("会员卡号："));
		h.add(s_cardno);
//		h.add(new Label("会员名称"));
//		h.add(cname);
		h.add(query);
		h.add(print);
		
		query.addListener(Events.Select, this);
		print.addListener(Events.Select, this);
		s_cardno.addListener(Events.KeyUp, this);
		return h;
	}


	/**
	 * 打印
	 */
	public void Print()
	{
		
		 if (GXT.isIE) {
			 printFrame_IE();
		 }
		 else
		 {
			 printFrame();
		 }
	}
	 /**
     * ie下的frame打印
     * 
     */
    
     static native void printFrame_IE()/*-{

    	$wnd.frames['__printingFrameX_Ccodequery_service'].focus();
    	$wnd.frames['__printingFrameX_Ccodequery_service'].print();
	}-*/;
	/**
	 * 非ie下的打印
	 */
     static native void printFrame() /*-{
    	

	var iframe = $doc.frames ? $doc.frames['__printingFrameX_Ccodequery_service'] : $doc.getElementById('__printingFrameX_Ccodequery_service');
    var ifWin = iframe.contentWindow || iframe;
    ifWin.print();


	}-*/;
     
     @Override
 	public void handleEvent(BaseEvent be) {
 		// TODO Auto-generated method stub
 		
 		if(be.getSource().equals(query))
 		{
 			query();
 			
 		}
 		else if(be.getSource().equals(print))
 		{
 			

 			Print();
 			
 		}
 		else if(be.getSource().equals(s_cardno))
 		{
 			if (((ComponentEvent) be).getKeyCode() == 13|| ((ComponentEvent) be).getKeyCode() == 10)
			{
 				query();
			}
 		}
 		
 	}
     
     void query()
     {
    		if(s_cardno.getValue()==null)
 			{
 				MessageBox.alert("错误", "请输入会员卡号", null);
 			}
 			else
 			frame.el().setElementAttribute("src", "./busijsp/ccode/ccodequery.jsp?s_cardno="+s_cardno.getValue());

     }
 	
}
